package com.dabbo.starship.block;

import java.rmi.RemoteException;

import com.dabbo.starship.cell.CellDefault;
import com.dabbo.starship.cell.CellFloor;
import com.dabbo.starship.interfaces.Block;
import com.dabbo.starship.interfaces.Cell;
import com.dabbo.starship.interfaces.CellLocationInterface;
import com.dabbo.starship.interfaces.Container;

public class BlockDefault extends java.rmi.server.UnicastRemoteObject implements Block {

	private static final long serialVersionUID = 7309925470112890308L;

	// XXXXXXXXX
	// XX     XX
	// X       X
	// X       X
	// X   C   X
	// X       X
	// X       X
	// XX     XX
	// XXXXXXXXX	

	protected Container parentContainer;
	protected String description = "Corridor";
	protected Cell[][] grid;
	protected boolean lighting = false;
	protected int blockType = 0;
	
	public BlockDefault(Container parent) throws RemoteException {
		this.parentContainer = parent;
		this.blockType = Block.BLOCK_DEFAULT;
		this.grid = new Cell[9][9];
		for (int y = 0; y < 9; y++) {
			for (int x = 0; x < 9; x++) {
				if (y == 0 || y == 8 || x == 0 || x == 8) {
					grid[y][x] = new CellDefault(this);	
				} else {
					grid[y][x] = new CellFloor(this);
				}
			}
		}
		grid[1][1] = new CellDefault(this);
		grid[1][7] = new CellDefault(this);
		grid[7][1] = new CellDefault(this);
		grid[7][7] = new CellDefault(this);
		//grid[4][4] = new CellConsole(this);
	}
	
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean isBlockLit() {
		return this.lighting;
	}
	public void setLighting(boolean lighting) {
		this.lighting = lighting;
	}
	
	public int getPowerOutput() throws RemoteException {
		int totalPower = 0;
		if (isBlockLit()) {
			totalPower = totalPower - 8;
		}
		for (int y = 0; y < 9; y++) {
			for (int x = 0; x < 9; x++) {
				totalPower = totalPower + grid[y][x].getPowerOutput();
			}
		}
		return totalPower;
	}

	public void activateCell(CellLocationInterface cellLocation) throws RemoteException {
		grid[cellLocation.getCoY()][cellLocation.getCoX()].activate();
	}


	public void deActivateCell(CellLocationInterface cellLocation) throws RemoteException {
		grid[cellLocation.getCoY()][cellLocation.getCoX()].deActivate();
	}
	
	public void tick() throws RemoteException {
		for (int y = 0; y < 8; y++) {
			for (int x = 0; x < 8; x++) {
				grid[y][x].tick();
			}
		}
	}
	
	public void powerFailure() throws RemoteException {
		this.lighting = false;
		for (int y = 0; y < 8; y++) {
			for (int x = 0; x < 8; x++) {
				grid[y][x].powerFailue();
			}
		}
	}

	@Override
	public Cell getCell(CellLocationInterface cellLocation) throws RemoteException {
		return grid[cellLocation.getCoY()][cellLocation.getCoX()];
	}

	@Override
	public int getBlockType() throws RemoteException {
		return this.blockType;
	}

	@Override
	public void setCell(Cell cell, CellLocationInterface cellLocation) throws RemoteException {
		grid[cellLocation.getCoY()][cellLocation.getCoX()] = cell;
	}

	@Override
	public Cell[][] getCells() throws RemoteException {
		return grid;
	}

	@Override
	public Container getParent() throws RemoteException {
		return this.parentContainer;
	}
	
}
